package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService  strategyStatisVORedisService;
    //攻略明细

    @RequestMapping("/detail")
    @ResponseBody
    public Object detail(String id){
        Strategy strategy = strategyService.findById(id);
        System.out.println(strategy);
        //阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }

    @RequestMapping("themes")
    @ResponseBody
    public Object themes(){
        return JsonResult.success(strategyThemeService.findAll());
    }

    @RequestMapping("query")
    @ResponseBody
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }




    @RequestMapping("/comments")
    @ResponseBody
    public Object comments(StrategyCommentQuery qo){
        qo.setPageSize(3);
        return JsonResult.success(strategyCommentService.query(qo));
    }




    @RequireLogin
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ResponseBody
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){

        //设置用户信息
        //同名属性才可以进行属性拷贝
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());  //用户id设置
        strategyCommentService.addComment(comment);
        //评论数+i
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(), 1);
        return JsonResult.success();
    }


    //cid 攻略评论id
    //sid 攻略id
    @RequireLogin
    @RequestMapping(value="/commentThumb",method = RequestMethod.POST)
    @ResponseBody
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo){

        //点赞
        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();
    }

    //攻略明细
    @RequestMapping(value="/statisVo",method = RequestMethod.GET)
    @ResponseBody
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }


    //攻略收藏
    @RequireLogin
    @RequestMapping(value="/favor",method = RequestMethod.POST)
    @ResponseBody
    public Object favor(String sid, @UserParam UserInfo userInfo){

        System.out.println(sid);
        //攻略收藏 : ret true 收藏成功， false表示取消收藏
        boolean ret = strategyStatisVORedisService.favor(sid, userInfo.getId());


        return JsonResult.success(ret);
    }
    //攻略点赞
    @RequireLogin
    @RequestMapping(value="/strategyThumbup",method = RequestMethod.POST)
    @ResponseBody
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){

        //攻略点赞 : ret true 点赞成功， false表示今天已经点过
        boolean ret = strategyStatisVORedisService.thumbup(sid, userInfo.getId());


        return JsonResult.success(ret);
    }



}
